let date = document.querySelector(".date");
let tbody = document.querySelector(".tbody");
let footer = document.querySelector("footer > div");
// let tr1 = document.querySelector(".tr1");
// let tr2 = document.querySelector(".tr2");
// let tr3 = document.querySelector(".tr3");
// console.log(sessionStorage.getItem("token"));
let day_time = [];
let day_time_arr = [];
let show_id_arr = [];
let show_name_arr = [];
let six_index = [];




ajax("http://newshopapi.0melon0.cn/api/f_user/chooselist", {
    "token": `${sessionStorage.getItem("token")}`
}, "GET").then(res1 => {
    // console.log(res1);
    res1.result.list.forEach(item => {
        // console.log(item);
        // console.log(item.day_time);
        date.innerHTML += ` <option value="${item.day_time}">${item.day_time}</option>`;
        day_time.push(item.day_time);
    });
});



if (sessionStorage.getItem("token")) {
    date.addEventListener("change", () => {
        // tr1.innerHTML = "";
        // tr2.innerHTML = "";
        // tr3.innerHTML = "";
        tbody.innerHTML = "";
        // footer.innerHTML = "";
        ajax("http://newshopapi.0melon0.cn/api/f_user/chooseday", { "token": `${sessionStorage.getItem("token")}`, "day_time": date.value, }, "GET").then(re => {
            // console.log(re);
            // tr1.innerHTML += `<div id="${re.result.list[0].show_id}">${re.result.list[0].show_name}</div>`;
            // tr2.innerHTML += `<div id="${re.result.list[1].show_id}">${re.result.list[1].show_name}</div>`;
            // tr3.innerHTML += `<div id="${re.result.list[2].show_id}">${re.result.list[2].show_name}</div>`;

            re.result.list.forEach(item => {
                // console.log(item);
                show_id_arr.push(item.show_id);
                day_time_arr.push(item.day_time);
                show_name_arr.push(item.show_name);

                six_index.push(item.show_id, item.day_time);
                // console.log(six_index);

                // console.log(show_name_arr);
                // console.log(show_id_arr);
                // console.log(day_time_arr);

                tbody.innerHTML += `<div id="${item.show_id} a "style="width:40vw; margin-top:12vw;">${item.show_name}</div>`;
                // console.log(item.show_name);


                let timestamp = Date.now();
                // console.log(timestamp);
                // console.log(item);
                let minTime = new Date(item.day_time + " " + item.show_name.split('-')[0]);
                // sessionStorage.setItem("minTime", minTime)
                let maxTime = new Date(item.day_time + " " + item.show_name.split('-')[1]).getTime();
                // console.log(minTime);
                // console.log(maxTime);
                // console.log(item.show_name.split('-')[0]);

                if (maxTime > timestamp && timestamp < minTime.getTime()) {
                    tbody.innerHTML += `<div id="a" style="width:38vw; margin-top:12vw;color:green;font-weight:border">可预约</div>`

                } else {
                    tbody.innerHTML += `<div style="width:38vw; margin-top:12vw;color:red">不可预约</div>`
                }
            })

            let divList = tbody.querySelectorAll("div")
                // footer.onclick = function() {
                //     console.log(divList);
                // }

            divList.forEach((item, index) => {

                item.addEventListener("click", e => {
                    // console.log(e.target);
                    console.log(index);
                    console.log(show_name_arr[0]);
                    if (e.target.innerHTML == "可预约") {
                        sessionStorage.setItem("show_id", show_id =
                            six_index[index - 1]);
                        // show_id_arr[index]);
                        if (index == 1) {
                            sessionStorage.setItem("show_name", show_name_arr[0])
                        }
                        if (index == 3) {
                            sessionStorage.setItem("show_name", show_name_arr[1])
                        }
                        if (index == 5) {
                            sessionStorage.setItem("show_name", show_name_arr[2])
                        }
                        // console.log(index);
                        sessionStorage.setItem("day_time", day_time =
                            six_index[index]);
                        // day_time_arr[index]);

                        // console.log(six_index);
                        // console.log(day_time = six_index[index]);

                        // sessionStorage.setItem("show_name", show_name = show_name_arr[index]);
                        // console.log(day_time_arr);


                        // console.log(show_id);

                        ajax("http://newshopapi.0melon0.cn/api/f_user/bookday", {
                            "token": `${sessionStorage.getItem("token")}`,
                            "day_time": `${sessionStorage.getItem("day_time")}`,
                            "show_id": `${sessionStorage.getItem("show_id")}`,
                        }, "POST").then(res => {
                            console.log(res.result.id);
                            console.log(res);

                            // sessionStorage.setItem("res.result.id")
                            ajax("http://newshopapi.0melon0.cn/api/f_user/Qrcode", {
                                "token": `${sessionStorage.getItem("token")}`,
                                "id": `${res.result.id}`
                            }, "GET").then(re => {
                                console.log(re);
                                sessionStorage.setItem("img", re.data.data);
                                window.location.href = "../成功预约/success.html";

                            })
                        })

                    }


                })
            })
        })


    })
}